package com.learn.algorithm.designpattern.singleton;

/**
 * @author wangxing
 * @version 2020/8/9 16:02 Administrator
 */
public class LazySingletSynchronizedThree {
    private static volatile LazySingletSynchronizedThree lazySingletSynchronizedOne;

    private LazySingletSynchronizedThree() {
    }

    /**
     * 处理了单次判定中的效率问题,只有初始的时候会锁线程,后续不需要进行锁也客户获取数据
     * 添加volatile关键字确保不会出现指令重排问题
     * @return
     */
    public static LazySingletSynchronizedThree getInstance() {
        if (lazySingletSynchronizedOne == null) {
            synchronized (LazySingletSynchronizedThree.class) {
                if (lazySingletSynchronizedOne == null) {
                    lazySingletSynchronizedOne = new LazySingletSynchronizedThree();
                }
            }
        }
        return lazySingletSynchronizedOne;
    }
}
